(function($){ 

var IconType = window.IconType = {  
    Error: "Error",  
    Info: "Info",
    Question:"Question",
    Warning:"Warning"
 };  
 
var dragFlag = false;
var startX = 0;
var startY = 0;
		 
$.Messager={
    Alert:function(options){
          var options = $.extend({},$.Messager.defaults, options);           
          Init("alert",options);
          BindEvent("alert",options);
          Show();
    },
    Confirm:function(options){
          var options = $.extend({},$.Messager.defaults, options); 
          Init("confirm",options);
          BindEvent("confirm",options);
          Show();    
    },
    Prompt:function(options){
         var options = $.extend({},$.Messager.defaults, options); 
          Init("prompt",options);
          BindEvent("prompt",options);
          Show();    
    }
 
 };

function Init(type,options)
{
       var strNode=" <div class='Msgbox' id='Mstar-Msgbox'>"+
       "<div class='transbg' id='Msgboxbg'>"
       + "<iframe class='transbg' id='MsgboxbgIframe' style='opacity:0.01;filter:alpha(opacity=1);border:0px '></iframe>"+"</div>" +
       "<div class='Msgbox-background' id='Mstar-Msgboxbg'>" +
       "   <div class='Msgbox-wrap'>" +
       "    <ul> "+
       "           <li class='Msgbox-header'>" +             
       "               <span class='Msgbox-title'>"+options.Title+"</span>" +
       "               <span class='Msgbox-quit'><input name='Quit' type='button' class='button-quit' id='Msgbox-btnQuit'/></span>"+
       "           </li>"+              
       "           <li class='Msgbox-content'>"+                
       "               <img src='../themes/Images/icon_"+options.Icon+".jpg' alt='"+options.Icon+"' class='img-layer' />"+
       "               <p class='Msgbox-content-p text-bold'> "+
                          window.location.protocol+"//"+window.location.host +
       "               </p>"+                 
       "               <p class='Msgbox-content-p'>"+
                        options.Message +
       "               </p>";
       
       if(type=="prompt")
         { 
          strNode+="<p class='Msgbox-content-p'><input type='text' style='width:250px' id='Mstar-MsgboxPromptTxt' /></p>" ;
          type="alert";
         }
         
       strNode+="   <p class='clean'></p>"+              
       "            </li>"+                  
       "            <li class='Msgbox-buttom'>"; 
         
       if(type=="confirm")
            strNode+=" <button class='button-small' id='Msgbox-btnOk'><span>Yes</span></button> " +               
                     " <button class='button-small' id='Msgbox-btnNo'><span>No</span></button>";
       else if(type=="alert")
           strNode+=" <button class='button-small text-bold'id='Msgbox-btnOk'><span>Ok</span></button> ";
           
       strNode+="</li>"+
            "</ul> "+     
          "</div> "+
      "</div>"+
    "</div>";

    Close();
    $("body").append(strNode);
    
}

function BindEvent(type,options)
{
  if (typeof(options.Oncallback) == "function" && $('#Mstar-Msgbox').length>0) { 
                             
        $("#Msgbox-btnOk").unbind('click').click(function(){
            if(type=="prompt")
              options.Oncallback($('#Mstar-MsgboxPromptTxt').val(),options.Parameter);
            else if(type=="confirm")    
              options.Oncallback(true,options.Parameter);
            Close();
            return false;
        });

        $("#Msgbox-btnNo").unbind('click').click(function(){
            options.Oncallback(false,options.Parameter);		          
            Close();                  
            return false;
        });

        $('#Msgbox-btnQuit').unbind('click').click(function(){
            Close(); 
            return false;
        });
        
       //Drag and Drop
       var moveX = 0,moveY = 0,moveTop,moveLeft = 0,moveable = true;
       var	cw = document.documentElement.clientWidth,ch = document.documentElement.clientHeight,est = document.documentElement.scrollTop; 
       var	sw = document.getElementById("Mstar-Msgboxbg").scrollWidth,sh = document.getElementById("Mstar-Msgboxbg").scrollHeight;
		if ($.browser.version == 6.0 ) {
			moveTop = est;
		}else {
			moveTop = 0;
		}
        $('.Msgbox-header').mousedown(function(evt){
            e = window.event?window.event:evt;
            var offset=$('#Mstar-Msgboxbg').offset();
		    var ol = offset.left, ot = offset.top-moveTop;
		    moveX = e.clientX-ol;
		    moveY = e.clientY-ot;
		    moveable = true;
		    $(document).mousemove(function(e){
		        if (moveable) {
				    e = window.event?window.event:e;
				    var x = e.clientX - moveX;
				    var y = e.clientY - moveY;
				    if ( x > 0 &&( x + sw < cw) && y > 0 && (y + sh < ch) ) {						
					    $('#Mstar-Msgboxbg').css({"left": x+"px" ,"top":parseInt(y+moveTop)+"px"});
				    }
				}
			})
	        $(document).mouseup (function () {moveable = false;});
	        $('#Mstar-Msgboxbg').bind("selectstart", function(e){return false;}) 
            return false;                	      
        }); /**/
  } 
  
}

function Show()
{  
  SetBackgroundSize();
  $('#Mstar-Msgbox').show();
  $('#Mstar-Msgbox .Msgbox-background').show();  
  SetPosition(); 
 // $("#Mstar-Msgboxbg").css("margin-top",-$("#Mstar-Msgboxbg").height()/2);
  //alert($("#Mstar-Msgboxbg").height()+"   "+ document.body.scrollHeight+" "+$("#Mstar-Msgboxbg").css("margin-top")); 
  $('select').attr("visibility","hidden"); 
}

function SetBackgroundSize()
{
    var bodyh = $(document).height(),
     windowh = $(window).height();
   // var bodyw=$(document).width();
    var clientW=document.body.clientWidth; //for Ie6,IE7
    if (document.documentElement && document.documentElement.clientWidth){  //for IE8
        clientW = document.documentElement.clientWidth;  
    }  
    $("#Msgboxbg").width(clientW);
    $("#MsgboxbgIframe").width(clientW);
    //if window height is greater than <body> height, use window height
    if (windowh > bodyh) {
       $("#Msgboxbg").height(windowh);
       $("#MsgboxbgIframe").height(windowh);
    }
    else {
      $("#Msgboxbg").height(bodyh);
      $("#MsgboxbgIframe").height(bodyh);
    }
}

function SetPosition()
{  
    var st=$(window).scrollTop();
    var sl=$(window).scrollLeft();
    var ch=0;
    if(!$.browser.opera)
       ch=$(window).height();
    else 
     ch=document.documentElement.clientHeight; //for opera
     
    var cw=$(window).width();
    var lightboxH=$("#Mstar-Msgboxbg").height();   
    var lightboxW=$("#Mstar-Msgboxbg").width(); 
     
    $("#Mstar-Msgboxbg").css("left",sl+(cw-lightboxW)/2);
    $("#Mstar-Msgboxbg").css("top",st+(ch-lightboxH)/2);   
}

//resets height when the window is resized
$(window).resize(function () {
    if( $("#Mstar-Msgboxbg").css("display")!="none"){ 
      SetBackgroundSize();  
      SetPosition();
    }
});

//resets height when the window is scrolled
$(window).scroll(function(){       
   if( $("#Mstar-Msgboxbg").css("display")!="none"){  
      SetBackgroundSize(); 
      SetPosition();
    }
})/**/


function Close()
{
   if($('#Mstar-Msgbox').length>0){
       //$('#Mstar-Msgbox .transbg').fadeOut("slow");
//       $('#Mstar-Msgbox .Msgbox-background').slideUp("fast");
//       $('#Mstar-Msgbox').fadeOut("slow");
      // $(document).remove("#Mstar-Msgbox");
      $("#Mstar-Msgbox").remove();
//       var selectedNode=document.getElementById("Mstar-Msgbox");                 
//       document.body.removeChild (selectedNode); 
    }
}

//plugin defaults - added as a property on our plugin function 
$.Messager.defaults={
       Title: 'Default Titile',    
       Message: 'This is a default message!',
       Icon:IconType.Info,
       Parameter:'',
       Oncallback:function(){}
};

})(jQuery); 
